package com.lin.pay.paypal.v2;

import com.lin.pay.paypal.v2.dto.orders.AmountBreakdown;
import com.lin.pay.paypal.v2.dto.orders.AmountWithBreakdown;
import com.lin.pay.paypal.v2.dto.orders.CreateOrderDto;
import com.lin.pay.paypal.v2.dto.orders.Money;
import com.lin.pay.paypal.v2.dto.orders.PurchaseUnit;
import com.lin.pay.paypal.v2.dto.orders.RefundDto;
import com.lin.properties.PayProperties;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.message.BasicHeader;

import java.util.ArrayList;
import java.util.List;

public class Helper {

    public static RefundDto buildRefundReq(String amt, String currency) {
        Money m = new Money();
        m.setCurrency_code(currency);
        m.setValue(amt);
        RefundDto rd = new RefundDto();
        rd.setAmount(m);
        return rd;
    }

    public static Header[] buildHeader(String accessToken) {
        return new Header[]{
                new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
                new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken),
                new BasicHeader("PayPal-Partner-Attribution-Id", PayProperties.BN_CODE),
                new BasicHeader("Prefer", "return=representation")
        };
    }

    public static CreateOrderDto buildCreateOrderRequestBody(String amt, String currency) {
        Money empty=new Money();
        empty.setValue("0");
        empty.setCurrency_code("USD");
        Money money=new Money();
        money.setValue(amt);
        money.setCurrency_code(currency);
        AmountBreakdown breakdown =new AmountBreakdown();
        breakdown.setShipping(money);
        breakdown.setItem_total(empty);
        breakdown.setShipping_discount(empty);
        breakdown.setDiscount(empty);
        breakdown.setHandling(empty);
        breakdown.setInsurance(empty);
        breakdown.setTax_total(empty);
        AmountWithBreakdown ab = new AmountWithBreakdown();
        ab.setCurrency_code(currency);
        ab.setValue(amt);
        ab.setBreakdown(breakdown);
        PurchaseUnit pu = new PurchaseUnit();
        pu.setReference_id("test_ref_id1");
        pu.setAmount(ab);
        List<PurchaseUnit> list = new ArrayList<>();
        list.add(pu);
        CreateOrderDto co = new CreateOrderDto();
        co.setIntent("CAPTURE");
        co.setPurchase_units(list);
        return co;
    }
//    public static OrderRequest buildCreateOrderRequestBody1(String amt, String currency){
//        return new OrderRequest()
//                .checkoutPaymentIntent("CAPTURE")
//                .purchaseUnits(new ArrayList<PurchaseUnitRequest>() {{
//                    add(new PurchaseUnitRequest()
//                            .referenceId("test_ref_id1")
//                            .amountWithBreakdown(new AmountWithBreakdown()
//                                    .currencyCode(currency) //"USD"
//                                    .value(amt))); //"100.00"
//                }});
//    }
}
